package com.the4man.cnbdw.action;

import com.opensymphony.xwork2.ModelDriven;
import com.the4man.cnbdw.cache.DeviceCache;
import com.the4man.cnbdw.service.EmpHeartrateInfoService;
import com.the4man.cnbdw.service.EmployeeService;
import com.the4man.cnbdw.service.LogInfoService;
import com.the4man.cnbdw.service.WarnInfoService;
import com.the4man.cnbdw.utils.PageUtil;
import com.the4man.framework.action.BaseAction;
import com.the4man.framework.model.*;
import com.the4man.framework.util.DateUtil;
import com.the4man.framework.util.LOG;
import com.the4man.framework.util.PageJson;
import com.the4man.framework.utils.GenUtil;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

@Action(
        value = "empHeartrateInfoAction",
        results = {@Result(
                name = "toImportPage",
                location = "/cnbdw/empHeartrateInfo/importPage.jsp"
        ), @Result(
                name = "listPage",
                location = "/cnbdw/empHeartrateInfo/list.jsp"
        ), @Result(
                name = "editPage",
                location = "/cnbdw/empHeartrateInfo/edit.jsp"
        )}
)
public class EmpHeartrateInfoAction extends BaseAction implements ModelDriven<EmpHeartrateInfo> {
    private EmpHeartrateInfo model = new EmpHeartrateInfo();
    private LogInfo logInfo = new LogInfo();
    @Autowired
    private LogInfoService logService;
    @Autowired
    private EmpHeartrateInfoService empHeartrateInfoService;
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private DeviceCache deviceCache;
    @Autowired
    private WarnInfoService warnInfoService;
    private String noHeartValFlag = "--";
    private File heartrateFile;
    private String heartrateFileFileName;
    String time = DateUtil.getInstance().formatDate(new Date());

    public EmpHeartrateInfoAction() {
    }

    public String indexPage() {
        return "listPage";
    }

    public String toImportPage() {
        return "toImportPage";
    }

    public void uploadHeartrate() {
        HttpServletRequest request = this.getRequest();
        String mac = request.getParameter("mac");
        String heartData = request.getParameter("heartData");
        String elec = request.getParameter("elec");
        PageJson resultJson = new PageJson();
        if(!StringUtils.isEmpty(mac) && !StringUtils.isEmpty(heartData)) {
            Employee emp = this.getEmployeeByMac(mac);
            if(emp == null) {
                resultJson.setReturnMsg("mac地址没有绑定人员，不可以上报!");
                this.writeJson(resultJson);
            } else {
                List<EmpHeartrateInfo> importList = new ArrayList();
                String decollator = "_";
                String lineDecollator = ",";

                try {
                    String patten = "yyyy-MM-dd HH:mm:ss";
                    String[] heartDataArray = heartData.split(lineDecollator);
                    String[] var15 = heartDataArray;
                    int var14 = heartDataArray.length;

                    for(int var13 = 0; var13 < var14; ++var13) {
                        String s = var15[var13];
                        if(!StringUtils.isEmpty(s) && !s.contains("__")) {
                            String[] lineArray = s.split(decollator);
                            if(lineArray.length == 3) {
                                String t = lineArray[0];
                                String value = lineArray[1];
                                if(this.noHeartValFlag.equals(value)) {
                                    value = "0";
                                }

                                String step = lineArray[2];
                                EmpHeartrateInfo item = new EmpHeartrateInfo();
                                item.setHeartId(GenUtil.genUUID());
                                item.setMac(mac);
                                item.setCreateTime(DateUtils.parseDate(t, new String[]{patten}));
                                item.setElec(Integer.valueOf(elec.trim()));
                                item.setHeartVal(Integer.valueOf(value.trim()));
                                item.setStep(Integer.valueOf(step));
                                item.setEmpId(emp.getId());
                                item.setEmpName(emp.getName());
                                item.setDeviceNo(this.deviceCache.getHardwareNo(mac));
                                importList.add(item);
                                if(Integer.valueOf(value.trim()).intValue() <= 40 || Integer.valueOf(value.trim()).intValue() >= 150) {
                                    WarnInfo warn = new WarnInfo();
                                    warn.setId(GenUtil.genUUID());
                                    warn.setDeviceMac(mac);
                                    warn.setHeartVal(value);
                                    warn.setEmpName(emp.getName());
                                    warn.setCreateTime(t);
                                    warn.setEmpRegion(emp.getRegionId());
                                    warn.setWarnType("心率报警");
                                    warn.setHandle("1");
                                    this.warnInfoService.insert(warn);
                                    this.logInfo.setId(UUID.randomUUID().toString());
                                    this.logInfo.setTime(t);
                                    this.logInfo.setUserName(emp.getName());
                                    this.logInfo.setUserId(emp.getName());
                                    this.logInfo.setFunc("人员信息");
                                    this.logInfo.setOp("人员添加");
                                    this.logInfo.setMemo("姓名:" + emp.getName() + " 心率:" + value);
                                    this.logService.insert(this.logInfo);
                                }
                            }
                        }
                    }

                    boolean resultFlag = this.empHeartrateInfoService.importtHeartrateInfo(importList);
                    if(resultFlag) {
                        resultJson.setReturnFlag(true);
                        resultJson.setReturnMsg("上报成功!");
                    } else {
                        resultJson.setReturnMsg("上报失败，服务端错误!");
                    }
                } catch (Exception var26) {
                    resultJson.setReturnMsg(var26.getMessage());
                    var26.printStackTrace();
                } finally {
                    this.writeJson(resultJson);
                }

            }
        } else {
            resultJson.setReturnMsg("参数为空，不可以上报!");
            this.writeJson(resultJson);
        }
    }

    public void stepRankingList() {
        PageJson resultJson = new PageJson();

        try {
            List<EmpHeartrateInfo> list = this.empHeartrateInfoService.stepRankingList();
            this.writeJson(list);
        } catch (Exception var3) {
            resultJson.setReturnMsg("获取失败");
            var3.printStackTrace();
        }

    }

    public void getAllDatasourceList() {
        int currentPage = this.getPage();
        int pageSize = this.getRows();
        HttpServletRequest request = this.getRequest();
        String empName = request.getParameter("empName");
        String start = request.getParameter("start");
        String end = request.getParameter("end");
        DataGrid d = new DataGrid();
        String empId = "";

        try {
            if(StringUtils.isNotEmpty(empName)) {
                empId = this.employeeService.selectById(empName);
            }

            int cnt = this.empHeartrateInfoService.getHeartrateInfoCnt(empId, this.getModel().getStart(), this.getModel().getEnd());
            PageUtil page = PageUtil.build(currentPage, pageSize, cnt);
            List<EmpHeartrateInfo> list = this.empHeartrateInfoService.getHeartrateInfoPage(page.getStartRows(), pageSize, empId, this.getModel().getStart(), this.getModel().getEnd());
            d.setRows(list);
            d.setTotal(Long.valueOf((long)cnt));
            this.writeJson(d);
        } catch (Exception var12) {
            var12.printStackTrace();
            d.setRows((List)null);
            d.setTotal(Long.valueOf(0L));
            this.writeJson(d);
        }

    }

    public void getEmpJSON() {
        List<Map<String, String>> list = new ArrayList();
        EmployeeExample example = new EmployeeExample();
        List empList = null;

        try {
            empList = this.employeeService.selectByExample(example);
        } catch (Exception var8) {
            var8.printStackTrace();
            return;
        }

        Map<String, String> mapAll = new HashMap();
        mapAll.put("id", "");
        mapAll.put("text", "全部");
        list.add(mapAll);
        Iterator var6 = empList.iterator();

        while(var6.hasNext()) {
            Employee employee = (Employee)var6.next();
            Map<String, String> map = new HashMap();
            map.put("id", employee.getId());
            map.put("text", employee.getName());
            list.add(map);
        }

        this.writeJson(list);
    }

    public void importHeartrate() {
        PageJson resultJson = new PageJson();
        File file = this.getHeartrateFile();
        String fNameMac = this.getHeartrateFileFileName();
        String mac = fNameMac.substring(0, fNameMac.lastIndexOf("."));
        Employee emp = this.getEmployeeByMac(mac);
        if(emp == null) {
            resultJson.setReturnMsg("mac地址没有绑定人员，不可以上传!");
            this.writeJson(resultJson);
        } else {
            BufferedReader br = null;
            List<EmpHeartrateInfo> importList = new ArrayList();
            String decollator = "_";

            try {
                br = new BufferedReader(new FileReader(file));
                String s = null;
                String patten = "yyyy-MM-dd HH:mm:ss";

                while((s = br.readLine()) != null) {
                    if(!StringUtils.isEmpty(s) && !s.contains("__")) {
                        String[] lineArray = s.split(decollator);
                        if(lineArray.length == 3) {
                            String time = lineArray[0];
                            String value = lineArray[1];
                            if(this.noHeartValFlag.equals(value)) {
                                value = "0";
                            }

                            String step = lineArray[2];
                            EmpHeartrateInfo item = new EmpHeartrateInfo();
                            item.setHeartId(GenUtil.genUUID());
                            item.setMac(mac);
                            item.setCreateTime(DateUtils.parseDate(time, new String[]{patten}));
                            item.setHeartVal(Integer.valueOf(value.trim()));
                            item.setStep(Integer.valueOf(step));
                            item.setEmpId(emp.getId());
                            item.setEmpName(emp.getName());
                            item.setDeviceNo(this.deviceCache.getHardwareNo(mac));
                            importList.add(item);
                        }
                    }
                }

                LOG.info("导入数据的总条数：" + importList.size(), new String[0]);
                boolean resultFlag = this.empHeartrateInfoService.importtHeartrateInfo(importList);
                if(resultFlag) {
                    resultJson.setReturnFlag(true);
                    resultJson.setReturnMsg("导入成功!");
                } else {
                    resultJson.setReturnMsg("导入失败，服务端错误!");
                }
            } catch (Exception var24) {
                resultJson.setReturnMsg(var24.getMessage());
                var24.printStackTrace();
            } finally {
                if(br != null) {
                    try {
                        br.close();
                    } catch (IOException var23) {
                        var23.printStackTrace();
                    }
                }

                this.writeJson(resultJson);
            }

        }
    }

    public File getHeartrateFile() {
        return this.heartrateFile;
    }

    public void setHeartrateFile(File heartrateFile) {
        this.heartrateFile = heartrateFile;
    }

    public String getHeartrateFileFileName() {
        return this.heartrateFileFileName;
    }

    public void setHeartrateFileFileName(String heartrateFileFileName) {
        this.heartrateFileFileName = heartrateFileFileName;
    }

    public EmpHeartrateInfo getModel() {
        return this.model;
    }

    private Employee getEmployeeByMac(String mac) {
        Employee result = null;

        try {
            List<Employee> list = this.employeeService.selectEmployeeByMac(mac);
            if(list != null && list.size() > 0) {
                result = (Employee)list.get(0);
            }
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        return result;
    }
}
